package com.gy.algorithm.carl.arrexa;

/**
 * @BelongsProject: javabase
 * @BelongsPackage: com.gy.algorithm.carl.arrexa
 * @Author: gy
 * @Date: 2024/10/23 13:59
 * @Description:
 */
public class RemoveElement27 {

    public static int removeElement(int[] nums, int val) {
        if(null == nums || nums.length == 0){
            return 0;
        }

        int left = 0;
        int right = 0;
        for (int i = 0; i < nums.length; i++) {
            if(nums[i] == val){
                right++;
            }else{
                nums[left++] = nums[right++];
            }
        }
        return left;
    }

    // 2 twice try
    public static int removeElement1(int[] nums, int val) {
        int low = 0;
        int fast = 0;
        while(fast < nums.length){
            while(fast < nums.length && nums[fast] == val){
                fast++;
            }
            if(fast < nums.length){
                nums[low++] = nums[fast++];
            }
        }
        return low;
    }
}
